package yuque.a1至20.CalculateEContinuously;

/**
 * ClassName: CalculateEContinuously1
 * Package: yuque.CalculateEContinuously
 * Description:
 *
 * @Author fjorid
 * @Create 2024/12/30 19:29
 * @Version 1.0
 */
public class CalculateEContinuously1 {
    public static void main(String[] args) {
        double e =1.0;
        double factorial =1.0;
        int n =1;
        while (true){
            for (int i = 1; i <=n ; i++) {
                factorial *= i;

            }
            n++;
            e += 1.0/factorial;
            factorial = 1.0;//factorial每次用完之后要归1，很重要。一些变量用完后重新赋值下次才能接着用。
            System.out.println(e);


        }


    }
}


/*public class CalculateEContinuously1 {
    public static void main(String[] args) {
        double e = 1.0; // e的初始值
        double factorial = 1.0; // 用于计算阶乘的变量，初始值为1（0!）
        int n = 1; // 项数，从1开始

        while (true) {
            factorial *= n; // 基于上一次的阶乘值计算当前的阶乘
            double term = 1.0 / factorial; // 计算当前项
            e += term; // 累加到e的值中
            System.out.println(e);

            // 检查是否达到了所需的精度，这里以1e-15为例


            n++; // 增加项数
        }


    }
}*/
